package sdk.dna.lens;

import sdk.dna.lens.data.ArticleInfo;
import sdk.dna.lens.db.BookmarkDataBaseHandler;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.Window;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

public class ArticleActivity extends Activity {

	private ArticleInfo info;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		getWindow().requestFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.activity_article);

		WebView wv = (WebView) findViewById(R.id.webview_article);
		final ProgressBar bar = (ProgressBar) findViewById(R.id.web_view_loading_progress_bar);

		Intent intent = getIntent();
		info = (ArticleInfo) intent.getSerializableExtra("data");
		
		wv.setWebViewClient(new WebViewClient());
		wv.getSettings().setJavaScriptEnabled(true);
		wv.loadUrl(info.url);
		
		wv.setWebChromeClient(new WebChromeClient() {

			@Override
			public void onProgressChanged(WebView view, int newProgress) {
				if (newProgress < 100) {
					bar.setVisibility(ProgressBar.VISIBLE);
				} else if (newProgress == 100) {
					bar.setVisibility(ProgressBar.GONE);
				}
				bar.setProgress(newProgress);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.article_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		switch (item.getItemId()) {
			case R.id.article_menu_bookmark:
				BookmarkDataBaseHandler.getInstance(getBaseContext()).addBookmark(info);
				break;
//			case R.id.article_menu_share:
//				
//				break;
		}

		return super.onMenuItemSelected(featureId, item);
	}
}
